from bs4 import BeautifulSoup

def trun_data(html_content):
    bs = BeautifulSoup(html_content, "html.parser")
    main_code = bs.select_one("#page_content").select("div")
    result = []

    for m in range(0, len(main_code), 3):
        table_name = main_code[m].select_one("h2").text
        tr = main_code[m].select_one("table").select("tr")

        col_names = []
        for i in range(1, len(tr)):
            col_names.append(tr[i].select("td")[0].text)

        result.append(formatString(table_name, col_names))

    return result


def formatString(table_name, col_names):
    result_string = table_name + " ("

    for col_name in col_names:
        result_string = result_string + col_name + ","

    result_string = result_string[0:-1]
    result_string = result_string + ")"

    return result_string


if __name__ == '__main__':
    fp = open("sql.html", "r", encoding = 'utf-8')
    html_content = fp.read()
    fp.close()
    result_list = trun_data(html_content)

    fp = open("ans.txt", "w", encoding = 'utf-8')
    for row in result_list:
        fp.write(row + '\n')
    fp.close()